package com.lagou.subjectfour;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Scanner;

/**
 * 学生信息管理系统
 */
public class StudentInfoSystem implements IStudentSystem {

    /**
     * 学生信息List
     */
    private List<Student> students = new ArrayList<>();

    /**
     * 系统介绍
     */
    public StudentInfoSystem() {
        System.out.println("---------------------------------------");
        System.out.println("           学生信息管理系统               ");
        System.out.println("---------------------------------------");
    }

    /**
     * 打印功能列表
     */
    public void FuncSelect() {
        System.out.println("1：增加学生信息");
        System.out.println("2：删除学生信息");
        System.out.println("3：修改学生信息");
        System.out.println("4：查找学生信息");
        System.out.println("5：打印所有学生信息");
        System.out.println("6：退出");
        System.out.print("请选择：");
    }

    /**
     * 选择执行方法
     *
     * @param i
     * @param sc
     */
    public void ChooseFun(int i, Scanner sc) {
        int num;
        String name;
        int age;
        Student student;
        switch (i) {
            case 1:
                System.out.print("请输入学生信息(学号 姓名 年龄)：");
                num = sc.nextInt();
                name = sc.next();
                age = sc.nextInt();
                AddStudent(new Student(num, name, age));
                break;
            case 2:
                System.out.print("请输入删除学生的学号：");
                num = sc.nextInt();
                student = FindStudent(num);
                if (student != null)
                    DelStudent(student);
                break;
            case 3:
                System.out.print("请输入更新的学生信息(学号 姓名 年龄)：");
                num = sc.nextInt();
                name = sc.next();
                age = sc.nextInt();
                UpdateStudent(new Student(num, name, age));
                break;
            case 4:
                System.out.print("请输入查找学生的学号：");
                num = sc.nextInt();
                student = FindStudent(num);
                if(student!=null)
                    System.out.println("当前学生信息：" + student);
                break;
            case 5:
                PrintStudent();
                break;
            case 6:
                break;
            default:
                System.out.println("重新输入！");
                break;
        }
    }

    /**
     * 增加学生
     *
     * @param student
     */
    @Override
    public void AddStudent(Student student) {
        if (students.contains(student))
            System.out.println("添加失败，已存在学生信息！");
        else {
            students.add(student);
            System.out.println("添加成功！");
        }
    }

    /**
     * 删除学生
     *
     * @param student
     */
    @Override
    public void DelStudent(Student student) {
        if (students.contains(student)) {
            students.remove(student);
            System.out.println("删除成功！");
        } else {
            System.out.println("删除失败，不存在学生信息！");
        }
    }

    /**
     * 更新学生
     *
     * @param student
     */
    @Override
    public void UpdateStudent(Student student) {
        int index = students.indexOf(student);
        if (index > -1) {
            students.set(index, student);
            System.out.println("更新成功！");
        } else
            System.out.println("更新失败，不存在学生信息！");

    }

    /**
     * 查找学生
     *
     * @param num
     */
    @Override
    public Student FindStudent(int num) {
        Optional<Student> studentOptional = students.stream().filter(item -> item.getNum() == num).findFirst();
        if (studentOptional.isPresent()) {
            Student st = studentOptional.get();
            return studentOptional.get();
        } else {
            System.out.println("不存在学生信息！");
            return null;
        }
    }

    /**
     * 打印所有学生
     */
    @Override
    public void PrintStudent() {
        for (Student st : students) {
            System.out.println(st);
        }
    }
}
